package com.maiji.cloud.utils;

import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;

import java.awt.Graphics;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.HeadlessException;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import javax.swing.ImageIcon;

public class UploadUtil {

    public static String upload (byte[] file_buff, String file_name, String file_ext_name, Long fileLength) {
        TrackerServer trackerServer = null;
        try {
            ClientGlobal.init("fdfs_client.conf");
            TrackerClient trackerClient = new TrackerClient();
            trackerServer = trackerClient.getConnection();

            StorageServer storageServer = null;
            StorageClient1 storageClient1  = new StorageClient1(trackerServer, storageServer);

            NameValuePair[] nameValuePairs = new NameValuePair[3];
            nameValuePairs[0] = new NameValuePair("fileName", file_name);
            nameValuePairs[1] = new NameValuePair("fileExtName", file_ext_name);
            nameValuePairs[2] = new NameValuePair("fileLenght", String.valueOf(fileLength));


            return storageClient1.upload_file1(file_buff, file_ext_name, nameValuePairs);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (trackerServer != null) trackerServer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }


    public static void download () {
        try {
            ClientGlobal.init("fdfs_client.conf");
            TrackerClient trackerClient = new TrackerClient();
            TrackerServer trackerServer = trackerClient.getConnection();

            StorageServer storageServer = null;
            StorageClient1 storageClient1  = new StorageClient1(trackerServer, storageServer);

            String file_id = "group1/M00/00/00/wKh7eVzsYWyATbCVAAPowCyuPOI698.jpg";
            byte[] bytes = storageClient1.download_file1(file_id);
            FileOutputStream fos = new FileOutputStream("beckham7.jpg");
            fos.write(bytes);
            trackerServer.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Map<String,Integer> getWidthOrHeightByImgURL(String url){
        String suffix = url.substring(url.lastIndexOf(".") + 1);
        try {
            URL urls = new URL(url);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            Image image = Toolkit.getDefaultToolkit().getImage(urls);
            BufferedImage  biOut = toBufferedImage(image);
            Map<String,Integer> map = new HashMap<String,Integer>();
            map.put("width", biOut.getWidth());
            map.put("height", biOut.getHeight());
            return map;
        } catch (Exception e) {
            return null;
        }
        
    }
    
    public static BufferedImage toBufferedImage(Image image) {  
        if (image instanceof BufferedImage) {  
            return (BufferedImage) image;  
        }  
        image = new ImageIcon(image).getImage();  
        BufferedImage bimage = null;  
        GraphicsEnvironment ge = GraphicsEnvironment  
                .getLocalGraphicsEnvironment();  
        try {  
            int transparency = Transparency.OPAQUE;  
            GraphicsDevice gs = ge.getDefaultScreenDevice();  
            GraphicsConfiguration gc = gs.getDefaultConfiguration();  
            bimage = gc.createCompatibleImage(image.getWidth(null),  
                    image.getHeight(null), transparency);  
        } catch (HeadlessException e) {  
        }  
        if (bimage == null) {  
            int type = BufferedImage.TYPE_INT_RGB;  
            bimage = new BufferedImage(image.getWidth(null),  
                    image.getHeight(null), type);  
        }  
        Graphics g = bimage.createGraphics();  
        g.drawImage(image, 0, 0, null);  
        g.dispose();  
       
        return bimage;  
    }  
    
    

}
